package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;
    @Resource
    private DishMapper dishMapper;

    // 批量插入套餐
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.insert(setmeal);

        List<SetmealDish> setMealDishes = setmealDTO.getSetmealDishes();
        setMealDishes.forEach(setMealDish -> {
            setMealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.insert(setMealDishes);
    }

    // 套餐分类查询
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    // 批量删除套餐
    @Transactional
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            int status = setmealMapper.getStatusById(id);
            if (status == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }

            // 批量删除套餐信息数据
            setmealMapper.deleteByIds(ids);

            // 批量删除套餐菜品关系
            setmealDishMapper.deleteBySetmealId(ids);
        }
    }

    // 更新操作: 相当于删除原有的套装所有数据, 插入更新的数据
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Long id = setmealDTO.getId();
        List<Long> ids = new ArrayList<Long>();
        ids.add(id);

        // 删除操作
        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteBySetmealId(ids);

        // 插入
        insert(setmealDTO);
    }

    // 通过 id 查询
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealByDishId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    // 套餐起售停售
    public void updateStatus(Integer status, Long id) {
        if (Objects.equals(status, StatusConstant.ENABLE)) {
            List<Dish> disheList = dishMapper.getBySetmealId(id);
            if (disheList != null && !disheList.isEmpty()) {
                disheList.forEach(dish -> {
                    if (Objects.equals(dish.getStatus(), StatusConstant.DISABLE)) {
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        setmealMapper.updateStatus(status, id);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
